import axios from 'axios'
// mport { Toast } from 'vant'
import { showLoadding, hideLoadding } from './loading'
import md5 from 'js-md5';
axios.defaults.timeout = 9000
axios.defaults.baseURL = 'http://49.4.114.58:9222/'
// axios.defaults.baseURL = 'http://jytpay.reallycare.cn:8080/APIAuth/api/redirectJsonRequest/'
axios.defaults.loaddingNumber = 0

// http request 拦截器
axios.interceptors.request.use(
  config => {
    console.log(config);
    console.log(config.data);

    if(config.url =='/recycle/sub_reservation_order' || config.url=='/comm/upload'){
      return config;
    }
    if(config.data != '' && config.data != undefined ){
      var params_arr = config.data.toString().split("&");
      if(params_arr.length>0){
        var time = new Date().getTime();
        params_arr.push("timestamp="+time);
        params_arr.sort();
        var param_s = "";
        for(var i = 0;i<params_arr.length;i++){
          param_s+=params_arr[i]+"&";
        }
        param_s =  param_s.substring(0,param_s.lastIndexOf("&"));
        var secret = "0C9BAA98A50C4DD78BFF5ADD8A863C80";//密钥
        console.log(param_s);
        var sign = md5(param_s+secret);
        var request_p = param_s+"&sign="+sign;
        config.data = request_p;
      }
    }






    /*if(config.timeout == 500){

    }*/
    // const token = getCookie('名称');注意使用的时候需要引入cookie方法，推荐js-cookie
    // config.data = JSON.stringify(config.data)
    // config.headers = {
    //   'Content-Type': 'application/x-www-form-urlencoded'
    // }
    // if(token){
    //   config.params = {'token':token}
    // }
    return config
  },
  error => {
    hideLoadding()
    return Promise.reject(error)
  }
)
// http response 拦截器
axios.interceptors.response.use(
  response => {
    axios.defaults.loaddingNumber--
    if (axios.defaults.loaddingNumber === 0) {
      hideLoadding()
    }
    return response
  },
  error => {
    hideLoadding()
    return Promise.reject(error)
  }
)

/**
 * 封装 post/get 请求
 * @param url
 * @param data
 * @param isloadding
 * @param method post / get
 * @returns {Promise}
 */

export function sendRequest (url, data = {}, isloadding = false, method = 'post') {

  if (isloadding && axios.defaults.loaddingNumber === 0) {
    showLoadding('正在加载...')
  }
  axios.defaults.loaddingNumber++

  return new Promise((resolve, reject) => {
    if (method === 'get') {
      axios.get(url, { params: data }).then(response => {
        resolve(response)
      }).catch(err => {
        reject(err)
      })
    } else {
      axios.post(url,data).then(response => {
        resolve(response)
      }, err => {
        reject(err)
      })
    }
  })
}

export function sendFileRequest (url, data = {}, isloadding = false, method = 'post') {

  if (isloadding && axios.defaults.loaddingNumber === 0) {
    showLoadding('正在加载...')
  }
  axios.defaults.loaddingNumber++
  return new Promise((resolve, reject) => {
    if (method === 'get') {
      axios.get(url, { params: data }).then(response => {
        resolve(response)
      }).catch(err => {
        reject(err)
      })
    } else {
      let config = {
        headers: {
          'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
        }
      };

      axios.post(url,data,config).then(response => {
        resolve(response)
      }, err => {
        reject(err)
      })
    }
  })
}
